<?php

namespace App\Http\Controllers\Api;

use App\Exports\FirstListExport;
use App\Exports\SecondListExport;
use App\Exports\WaitingHrListExport;
use App\Exports\WaitingListExport;
use App\Exports\WaitingSecondListExport;
use App\Models\Batch;
use App\Models\Job;
use App\Models\Talent;
use App\Transformers\TalentTransfromer;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class InterviewController extends Controller
{
    public function waiting(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 1)->where('offer_status', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('wait_status') || $request->input('wait_status') === 0 || $request->input('wait_status') === '0') {
            $talent->where('wait_status', $request->input('wait_status'));
        }
        if ($request->input('batch')) {
            $talent->where('batch', $request->input('batch'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $talent->where('sex', $request->input('sex'));
        }

        $talents = $talent->orderBy('updated_at', 'desc')->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function export_waiting(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $excel = '待面试-'.$datetime.'.xlsx';
            return Excel::download(new WaitingListExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function waiting_second(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 5)->where('offer_status', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('wait_status2') || $request->input('wait_status2') === 0 || $request->input('wait_status2') === '0') {
            $talent->where('wait_status2', $request->input('wait_status2'));
        }
        if ($request->input('batch')) {
            $talent->where('batch', $request->input('batch'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $talent->where('sex', $request->input('sex'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function export_waiting_second(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $excel = '待二面-'.$datetime.'.xlsx';
            return Excel::download(new WaitingSecondListExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function waiting_hr(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 9)->where('offer_status', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('wait_status3') || $request->input('wait_status3') === 0 || $request->input('wait_status3') === '0') {
            $talent->where('wait_status3', $request->input('wait_status3'));
        }
        if ($request->input('batch')) {
            $talent->where('batch', $request->input('batch'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $talent->where('sex', $request->input('sex'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function export_waiting_hr(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $excel = '待HR面-'.$datetime.'.xlsx';
            return Excel::download(new WaitingHrListExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function waiting_quick(Request $request, Talent $talent)
    {
        $post = '';
        if (preg_match('/^\d*$/', $request->input('job_id'))) {
            $job = Job::find($request->input('job_id'));
//            $post = $job->project->post;
            $post = $job->post;
        } else {
            $post = $request->input('job_id');
        }
        $query = $talent->query()
            ->where('interview_status', 1)
            ->where('recommend', $post);
        $list = $query->paginate(20);

        return $this->response->paginator($list, new TalentTransfromer());
    }

    public function waiting_update(Talent $talent, Request $request)
    {
//        if ((int) $request->wait_status === 1) {
//            $talent->update([
//                'wait_status' => $request->wait_status,
//                'interview_status' => 2,
//            ]);
//        } else {
//            $talent->update([
//                'wait_status' => $request->wait_status,
//            ]);
//        }
        switch ($request->input('step')) {
            case 'first':
                $talent->update([
                    'wait_status' => $request->input('wait_status')
                ]);
                break;
            case 'second':
                $talent->update([
                    'wait_status2' => $request->input('wait_status')
                ]);
                break;
            case 'hr':
                $talent->update([
                    'wait_status3' => $request->input('wait_status')
                ]);
                break;
        }
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function first(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
//        $talent->whereIn('interview_status', [2, 3, 4]);
        $talent->where('interview_status', 2);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $batch = Batch::where('name', $request->input('batch'))->first();
            if ($batch) {
                $talent->where('first_batch_id', $batch->id);
            }
        }
        if ($request->input('first_interviewer')) {
            $talent->where('first_interviewer', $request->input('first_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }
        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function export_first(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $excel = '一面-'.$datetime.'.xlsx';
            return Excel::download(new FirstListExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function second(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
//        $talent->whereIn('interview_status', [6, 7, 8]);
        $talent->where('interview_status', 6);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $batch = Batch::where('name', $request->input('batch'))->first();
            if ($batch) {
                $talent->where('second_batch_id', $batch->id);
            }
        }
        if ($request->input('second_interviewer')) {
            $talent->where('second_interviewer', $request->input('second_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function export_second(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $excel = '二面-'.$datetime.'.xlsx';
            return Excel::download(new SecondListExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function hr(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 10);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $talent->where('batch', $request->input('batch'));
        }
        if ($request->input('second_interviewer')) {
            $talent->where('second_interviewer', $request->input('second_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function hr_a(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 10)->where('tan_xin_status', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $batch = Batch::where('name', $request->input('batch'))->first();
            if ($batch) {
                $talent->where('hr_batch_id', $batch->id);
            }
        }
        if ($request->input('second_interviewer')) {
            $talent->where('second_interviewer', $request->input('second_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function hr_b(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 10)->where('tan_xin_status', 1)->where('tijian', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $batch = Batch::where('name', $request->input('batch'))->first();
            if ($batch) {
                $talent->where('hr_batch_id', $batch->id);
            }
        }
        if ($request->input('second_interviewer')) {
            $talent->where('second_interviewer', $request->input('second_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function hr_c(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 10)->where('tan_xin_status', 1)->where('tijian', 1)->where('send_offer', 0);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $batch = Batch::where('name', $request->input('batch'))->first();
            if ($batch) {
                $talent->where('hr_batch_id', $batch->id);
            }
        }
        if ($request->input('second_interviewer')) {
            $talent->where('second_interviewer', $request->input('second_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function back_pool(Talent $talent)
    {
        $talent->update([
            'interview_status' => 0,
            'wait_status' => 0,
            'offer_status' => 0,
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function first_comment(Request $request, Talent $talent)
    {
        $talent->update([
            'first_gzdl' => (int) $request->input('t1'),
            'first_zyjn' => (int) $request->input('t2'),
            'first_xmjy' => (int) $request->input('t3'),
            'first_xxcx' => (int) $request->input('t4'),
            'first_zxls' => (int) $request->input('t5'),
            'first_total' => (int) $request->input('total'),
            'first_comment' => $request->input('comment'),
            'interview_status' => (int) $request->input('status'),
            'first_interviewer' => $request->input('boss'),
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function second_comment(Request $request, Talent $talent)
    {
        $talent->update([
            'second_zysy' => (int) $request->input('t1'),
            'second_zynl' => (int) $request->input('t2'),
            'second_gzsl' => (int) $request->input('t3'),
            'second_gtxz' => (int) $request->input('t4'),
            'second_total' => (int) $request->input('total'),
            'second_comment' => $request->input('comment'),
            'interview_status' => (int) $request->input('status'),
            'second_interviewer' => $request->input('boss') ?? '',
            'is_system_in' => $request->input('is_system_in'),
            'system_in_level' => $request->input('system_in_level') ?? '',
            'recommend_level' => $request->input('recommend_level') ?? '',
            'ability' => $request->input('ability'),
            'recommend_salary' => $request->input('recommend_salary'),
            'salary_a' => $request->input('salary_a'),
            'salary_b' => $request->input('salary_b'),
            'offer_status' => $request->input('offer') === true ? 1 : 0,
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function hr_comment(Request $request, Talent $talent)
    {
        $tan_xin_status = $request->input('tan_xin_status') === true ? 1 : 2;
        $tijian = $request->input('tijian') === true ? 1 : 2;
        $send_offer = $request->input('send_offer') === true ? 1 : 2;
        $offer = $request->input('offer') === true ? 1 : 0;
        $tan_xin_reason = $request->input('tan_xin_reason') ? $request->input('tan_xin_reason') : '';
        $tijian_reason = $request->input('tijian_reason') ? $request->input('tijian_reason') : '';
        $reject_offer_reason = $request->input('reject_offer_reason') ? $request->input('reject_offer_reason') : '';

        $talent->update([
            'tan_xin_status' => $tan_xin_status,
            'tan_xin_reason' => $tan_xin_reason,
            'tan_xin' => $request->input('tan_xin'),
            'tan_xin_a' => $request->input('tan_xin_a'),
            'tan_xin_b' => $request->input('tan_xin_b'),
            'tijian' => $tijian,
            'tijian_reason' => $tijian_reason,
            'send_offer' => $send_offer,
            'reject_offer_reason' => $reject_offer_reason,
            'offer_status' => $offer,
            'interview_status' => $request->input('status'),
        ]);

        return $this->response->item($talent, new TalentTransfromer());
    }

    public function hr_a_comment(Request $request, Talent $talent)
    {
        $tan_xin_status = $request->input('tan_xin_status') === true ? 1 : 2;
        $tan_xin_reason = $request->input('tan_xin_reason') ? $request->input('tan_xin_reason') : '';

        $interview_status = 10;
        if ($tan_xin_status === 2) {
            $interview_status = 11;
        }

        $talent->update([
            'tan_xin_status' => $tan_xin_status,
            'tan_xin_reason' => $tan_xin_reason,
            'tan_xin' => $request->input('tan_xin'),
            'tan_xin_a' => $request->input('tan_xin_a'),
            'tan_xin_b' => $request->input('tan_xin_b'),
            'interview_status' => $interview_status,
        ]);

        return $this->response->item($talent, new TalentTransfromer());
    }

    public function hr_b_comment(Request $request, Talent $talent)
    {
        $tijian = $request->input('tijian') === true ? 1 : 2;
        $tijian_reason = $request->input('tijian_reason') ? $request->input('tijian_reason') : '';

        $interview_status = 10;
        if ($tijian === 2) {
            $interview_status = 11;
        }

        $talent->update([
            'tijian' => $tijian,
            'tijian_reason' => $tijian_reason,
            'interview_status' => $interview_status,
        ]);

        return $this->response->item($talent, new TalentTransfromer());
    }

    public function hr_c_comment(Request $request, Talent $talent)
    {
        $send_offer = $request->input('send_offer') === true ? 1 : 2;
        $reject_offer_reason = $request->input('reject_offer_reason') ? $request->input('reject_offer_reason') : '';
        $offerStatus = 0;
        $interviewStatus = 11;
        if ($send_offer === 1) {
            $offerStatus = 1;
            $interviewStatus = 13;
        }

        $talent->update([
            'send_offer' => $send_offer,
            'reject_offer_reason' => $reject_offer_reason,
            'offer_status' => $offerStatus,
            'interview_status' => $interviewStatus,
        ]);

        return $this->response->item($talent, new TalentTransfromer());
    }

    public function offer_success(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', 13)->where('send_offer', 1)
            ->where('tijian', 1)->where('tan_xin_status', 1)->where('offer_status', 1);
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('batch')) {
            $talent->where('batch', $request->input('batch'));
        }
        if ($request->input('second_interviewer')) {
            $talent->where('second_interviewer', $request->input('second_interviewer'));
        }
        if ($request->input('interview_status')) {
            $talent->where('interview_status', $request->input('interview_status'));
        }

        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }
}
